package com.bianguo.android.iocdemo.view.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bianguo.android.iocdemo.R;
import com.bianguo.android.iocdemo.adapter.PlayerAdapter;
import com.bianguo.android.iocdemo.ui.UIHelper;
import com.bianguo.android.iocdemo.ui.ViewCalculateUtil;
import com.bianguo.android.iocdemo.utils.BitmapUtil;

import java.util.ArrayList;

public class DiscView extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;
    private ImageView musicCircle;
    private ImageView musicNeedle;
    private PlayerAdapter adapter;
    private ArrayList<View> mDiscLayouts;
    private ArrayList<Integer> mMusicBgIds;
    private ArrayList<ObjectAnimator> mDiscAnimators;
    private ObjectAnimator mNeedleAnimator;
    private OnPageChangeListener onPageChangeListener;

    public DiscView(Context context) {
        this(context, null);
    }

    public DiscView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DiscView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mMusicBgIds = new ArrayList<>();
        mDiscAnimators = new ArrayList<>();
        mDiscLayouts = new ArrayList<>();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void playDiscAnimator(int position) {
        ObjectAnimator animator = mDiscAnimators.get(position);
        if (animator.isPaused()) {
            animator.resume();
        } else {
            animator.start();
        }
    }

    public void setMusicDataList(ArrayList<Integer> musicDataList) {
        if (musicDataList.isEmpty()) {
            return;
        }
        mDiscAnimators.clear();
        mMusicBgIds.clear();
        mDiscAnimators.clear();
        mMusicBgIds.addAll(musicDataList);
        for (Integer resId : mMusicBgIds) {
            View containerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_disc, mViewPager, false);
            ImageView imageView = containerView.findViewById(R.id.music_img);
            Drawable drawable = BitmapUtil.getMusicItemDrawable(getContext(), resId);
            ViewCalculateUtil.setLinearLayoutParams(imageView, 800, 800, ((1000 - 800) / 2) + 190, 0, 0, 0);
            imageView.setImageDrawable(drawable);
            mDiscLayouts.add(containerView);
            ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(imageView, View.ROTATION, 360, 0);
            rotateAnimator.setInterpolator(new LinearInterpolator());
            rotateAnimator.setDuration(20 * 1000);
            rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mDiscAnimators.add(rotateAnimator);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findView();
        mNeedleAnimator = ObjectAnimator.ofFloat(musicNeedle, View.ROTATION, -30, 0);
        mNeedleAnimator.setDuration(500);
        mNeedleAnimator.setInterpolator(new AccelerateInterpolator());
        mNeedleAnimator.addListener(new AnimatorListenerAdapter() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                playDiscAnimator(mViewPager.getCurrentItem());
            }
        });
    }

    private void findView() {
        mViewPager = findViewById(R.id.viewPager);
        musicCircle = findViewById(R.id.musicCircle);
        musicNeedle = findViewById(R.id.musicNeedle);

        int musicCircleWidth = UIHelper.getInstance().getWidth(813);
        Bitmap bitmapDisc = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R
                .drawable.ic_disc_blackground), musicCircleWidth, musicCircleWidth, false);

        RoundedBitmapDrawable roundDiscDrawable = RoundedBitmapDrawableFactory.create
                (getResources(), bitmapDisc);
        musicCircle.setImageDrawable(roundDiscDrawable);
        ViewCalculateUtil.setRelativeLayoutParams(musicCircle, 1000, 1000, 190, 0, 0, 0);

        ViewCalculateUtil.setRelativeLayoutParams(musicNeedle, 276, 413, 43, 0, 500, 0);
        musicNeedle.setPivotX(UIHelper.getInstance().getWidth(43));
        musicNeedle.setPivotY(UIHelper.getInstance().getHeight(43));

        adapter = new PlayerAdapter(mDiscLayouts);
        mViewPager.setAdapter(adapter);
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageChanged(position);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_IDLE:
                break;
            // 1 表示开始滑动
            case ViewPager.SCROLL_STATE_DRAGGING:
                pauseAnimator();
                break;
            // 2 表示结束滑动
            case ViewPager.SCROLL_STATE_SETTLING:
                startAnimator();
                break;

        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void pauseAnimator() {
        ObjectAnimator animator = mDiscAnimators.get(mViewPager.getCurrentItem());
        animator.pause();
        mNeedleAnimator.reverse();
    }

    private void startAnimator() {
        mNeedleAnimator.start();
    }

    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    public interface OnPageChangeListener {

        void onPageChanged(int position);
    }
}
